package org.iteam.xlz.core.service.product;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.product.ProductTags;

public class ProductTagsService extends BaseService<ProductTags>{
	static ProductTagsService instance;
	private ProductTagsService(){}
	
	public static ProductTagsService getInstance(){
		if(null==instance){
			instance=new ProductTagsService();
		}
		return instance;
	}
	
	public List<ProductTags> getProductTags(String pid){
		try {
			return super.query("select * from product_tags where 1=1 and pid=?",pid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ProductTags>();
		}
	}
	
	public String create(ProductTags model){
		String id=UUID.randomUUID().toString().replace("-","").toUpperCase();
		model.setId(id);
		SqlBuilder builder=SqlBuilder.createInsert("product_tags");
		builder.addField("id",model.getId());
		builder.addField("tag_text",model.getTag_text());
		builder.addField("tag_color",model.getTag_color());
		builder.addField("is_main",model.getIs_main());
		builder.addField("pid",model.getPid());
		try {
			if(super.updates(builder.getSql(),builder.getParams())>0){
				return id;
			}else{
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean update(ProductTags model){
		SqlBuilder builder=SqlBuilder.createUpdate("product_tags");
		builder.addField("is_main",model.getIs_main());
		builder.addField("tag_color",model.getTag_color());
		builder.addField("tag_text",model.getTag_text());
		builder.addWhere("id",model.getId());	
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean deleteProductTags(ProductTags model){
		Connection con = null;
		try {
			con = super.getConnection();
			return super.updates(con,"delete from product_tags where id=?",model.getId()) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}		